package org.unisannio.sc;

import java.util.List;

import org.unisannio.remote.FactoryRemote;
import org.unisannio.remote.RemoteContentProvider;
import org.unisannio.util.Converter;
import org.unisannio.util.ServiceInfo;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

public class ContactsActivity extends ListActivity {
	public static RemoteContentProvider contentRemote;
	private static final int SHARE_ID = Menu.FIRST+1;
	private static final int UNSHARE_ID = Menu.FIRST+2;
		
		private ContactService contactService;
		private Cursor cursor;
		private ContactAdapter contactAdapter;
		private List<Contact> contacts;
		
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			contactService=new ContactService();

				
				
				contentRemote=  new FactoryRemote().getRemoteContentProvider(ContactsActivity.this,3000);
				
			if (contentRemote.getServerAddress()==null){
				contentRemote.setServer("192.168.1.6:2222");
				ServiceInfo sinfo=new ServiceInfo();
				sinfo.addDescriptor(Data.CONTENT_URI.toString(), "contacts", "", ServiceInfo.CONTENT_PROVIDER);
				
				
				contentRemote.sendToSuperPeer(sinfo, Converter.SUBSCRIBE);
				contentRemote.sendToSuperPeer(sinfo, Converter.PUBLISH);
				
			}
			
			processGet();
			registerForContextMenu(getListView());
			
			
			
		}
		
		
		@Override
		protected void onResume() {
			processGet();
			super.onResume();
		}
		
		
		@Override
		public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
			menu.add(Menu.NONE, SHARE_ID, Menu.NONE, "Share");
			menu.add(Menu.NONE, UNSHARE_ID, Menu.NONE, "Hide");
		}

		@Override
		public boolean onContextItemSelected(MenuItem item) {
			AdapterView.AdapterContextMenuInfo info;
			switch (item.getItemId()) {
				case SHARE_ID:
					info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
					cursor.moveToPosition((int)info.position);	
					contactService.shareContact(cursor,getContentResolver());
//					processGet();
					contacts.get(info.position).setLabel("shared");
					contactAdapter.notifyDataSetChanged();
					return(true);
				case UNSHARE_ID:
					info=
						(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
					cursor.moveToPosition((int)info.position);
					contactService.hideContact(cursor,getContentResolver());
					contacts.get(info.position).setLabel("hide");
					contactAdapter.notifyDataSetChanged();
//					processGet();
					return(true);
			}

			return(super.onOptionsItemSelected(item));
		}

		
		
		
		private  void processGet(){
			ContentResolver cr=getContentResolver();
			cursor=cr.query(Data.CONTENT_URI, null,Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.NUMBER + " IS NOT NULL",null,Data.DISPLAY_NAME+" ASC");
//			String[] name=new String[1];
//				name[0]=	"Adalgiso Verdi";
//			cursor=cr.query(Data.CONTENT_URI, null,
//					Data.DISPLAY_NAME+ " LIKE ? ", name, null);
//			
	        
	        contacts=contactService.getContactsList(cursor);
	        contactAdapter=new ContactAdapter(this,R.layout.grid, contacts);
	        setListAdapter(contactAdapter);

		}
		
		
		
		
//		private void checkConnection(){
//			ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
//			NetworkInfo ninfo=conMgr.getActiveNetworkInfo();
//			if (ninfo==null || !(ninfo.isConnected())) {
//				AlertDialog.Builder builder = new AlertDialog.Builder(ContactsActivity.this);
//				builder.setMessage("Controlla di aver attivato una connessione dati valida.");
//				builder.setCancelable(false);
//				builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
//				           public void onClick(DialogInterface dialog, int id) {
//				               dialog.cancel();
//				           }
//				       });
//				AlertDialog alert = builder.create();
//				alert.show();
//			}
//		}
		
		
		@Override
		public void onBackPressed() {
			super.onBackPressed();
			setResult(RESULT_OK,null);
		}
		
		
		

		
		}
		
		
		
		
		
//		@Override
//		protected void onCreate(Bundle savedInstanceState) {
//			// TODO Auto-generated method stub
//			super.onCreate(savedInstanceState);
//			ContentResolver cr=getContentResolver(); 
//			ContentValues values = new ContentValues();
//			Cursor cursor;
//			   Uri uri=null,uriContact=null;;
//			  
////			   values.put(Data.DISPLAY_NAME, "Raw");
//				uriContact=cr.insert(RawContacts.CONTENT_URI, values);
//				long rawContactId = ContentUris.parseId(uriContact);
//				Log.d("client", ""+rawContactId);
//				
//				   values.clear();
//				   values.put(Phone.NUMBER, 33333);
//				   values.put(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
//				   values.put(ContactsContract.CommonDataKinds.Phone.TYPE,
//	               ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
//				   
//				   values.put(Data.RAW_CONTACT_ID, rawContactId);
//				  
//				   uri=cr.insert(Data.CONTENT_URI, values);
//				 
//				   
//				   
//				   values.put(Phone.NUMBER, 55555);
//				   values.put(Phone.LABEL, "customLabel");
//				   values.put(Phone.MIMETYPE, BaseTypes.TYPE_CUSTOM);
//				   values.put(Data.RAW_CONTACT_ID, rawContactId);
//					  
//				   uri=cr.insert(Data.CONTENT_URI, values);
//				   
//				   cursor=cr.query(uri, null, null, null, null);
//				   cursor.getColumnNames();
//				   cursor.moveToFirst();
//				   int i=cursor.getColumnIndex(Phone.LABEL);
//				   String numberRead=cursor.getString(i);
//				   values.clear();
//				   values.put(StructuredName.DISPLAY_NAME, "AAAProvaLabel");
//				   values.put(StructuredName.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE);
//				   values.put(Data.RAW_CONTACT_ID, rawContactId);
//				   uri=cr.insert(Data.CONTENT_URI, values);
//				   
//				   cursor=cr.query(uri, null, null, null, null);
//				   cursor.getColumnNames();
////				   values.clear();
////				   values.put(Data.MIMETYPE, Data.CONTENT_TYPE);
////				   values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,"Alessandro");
////				   values.put(Data.RAW_CONTACT_ID, rawContactId);
////				   cr.insert(Data.CONTENT_URI, values);
////				   
////				  
////				   values.clear();
////				   values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
////				   values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Alessandro");
////				   values.put(Data.RAW_CONTACT_ID, rawContactId);
////				    uri=cr.insert(Data.CONTENT_URI, values);
//			
//			  }
		
		
		
		

